package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"main.go/database"
	"net/http"
	"project/model"
	"strconv"
)

//查找
func find_goods(db *gorm.DB, request model.goods) bool {
	var goods model.goods
	db.Where("goods_id = ?", request.goods_id).First(&goods)
	if goods.port_company_name != "" {
		fmt.Println("查找成功")
		return true
	}
	fmt.Println("查找失败")
	return false
}

//插入
func insert_goods(db *gorm.DB, request model.goods) bool {
	if find_goods(db, request) == true {
		fmt.Println("该货物已存在，不能插入")
		return false
	}
	db.Create(&request) //创建一个元组
	fmt.Println("插入成功")
	return true
}

//删除
func delete_goods(db *grom.DB, request model.goods) bool {
	if find_goods(db, request) == false {
		fmt.Println("不存在该货物，无法删除")
		return false
	}
	db.Where("goods_id = ?", request.goods_id).Delete(&request)
	fmt.Println("删除成功")
	return true
}

//更新
func update_goods(db *grom.DB, request model.goods) bool {
	if find_goods(db, request) == false {
		fmt.Println("无该货物，无法更新")
		return false
	}
	db.Model(&request).Where("goods_id = ?", request.goods_id).Updates(
		model.goods{
			goods_id : request.goods_id
			goods_count : request.goods_count
			goods_tun : request.goods_tun
		},
	)
	fmt.Println("更新成功")
	return true
}
func web_show_goods(c *gin.Context)  {
	db = database.GetDB()
	var []goods model.goods
	result := db.Table("goods").Find(&goods)
	if result != nil {
		fmt.Println("!!")
	}
	fmt.Println(goods)
	c.JSON(http.StatusOK, gin.H{ //多传一些参数 可能会用到
		"code":             "200",
		"car": 				goods, //试试传一个列表
		"msg":              "成功",
	})
}

func web_get_goods(c *gin.Context)  {
	db := database.GetDB()
	var goods1 model.goods
	var goods2 = model.goods
	request := make(map[string]interface{})

	c.ShouldBind(&request)
	fmt.Println(request)
	car1.car_id = request["id"].(string)
	db.Where("goods_id = ?", goods1.car_id).First(&goods2)
	if goods2.goods_id != "" {
		fmt.Println("成功查找到货物")
		c.JSON(200, gin.H{
			"code":             "200",
			"purchase_details": car2, //传一个对象
			"msg":              "成功",
		})
	} else {
		c.JSON(404, gin.H{
			"code":             "404",
			"car_id": 			car2, //传一个空对象
			"msg":              "no",
		})
		fmt.Println("未找到该货物")
	}
}
func web_add_goods(c *gin.Context)  {
	db := database.GetDB()
	var goods model.goods
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	goods.goods_id = request["goods_id"].(string)
	goods.goods_count = strconv.Atoi(request["goods_count"].(string))
	goods.goods_tun = strconv.Atoi(request["goods_tun"].(string))
	fmt.Println(request)
	if insert_goods(db,goods) {
		c.JSON(200,gin.H{
			"code" : "200",
			"msg" : "成功",
		})
		fmt.Println("添加成功")
	}else{
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("添加失败")
	}
}
func web_delete_goods(c *gin.Context)  {
	db := database.GetDB()
	var goods model.goods
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	goods.goods_id = request["goods_id"].(string)
	if delete_goods(db, goods){
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "删除成功",
		})
		fmt.Println("删除成功")
	}else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "删除失败",
		})
		fmt.Println("删除失败")
	}
}
func web_update_goods(c *gin.Context)  {
	db := database.GetDB()
	var goods model.goods
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	goods.goods_id = request["goods_id"].(string)
	goods.goods_count = strconv.Atoi(request["goods_count"].(string))
	goods.goods_tun = strconv.Atoi(request["goods_tun"].(string))
	if update_goods(db, goods){
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "更新成功",
		})
		fmt.Println("更新成功")
	}else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "更新失败",
		})
		fmt.Println("更新失败")
	}
}
